package Model;
/*
 * Written by Phat Le and Dustin Stripplin
 */
import java.util.ArrayList;
import java.util.TreeSet;
import javax.swing.JTable;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import FileParser.FileParser;
import java.io.IOException;
import java.io.InputStream;
import TableModels.SPCTemplate;
/**
 * SubProgram Chair can assign paper to reviewer, make recommendation,
 * view manuscripts and reviews.
 * @author Phat and Dustin
 *
 */
public class SubProgramChair extends User {

	
	/**
	 * 
	 */
	private String name;
	/**
	 * 
	 */
	private Conference conference;
	/**
	 * 
	 */
	private String email;
	/**
	 * 
	 */
	private Integer phone;
	/**
	 * 
	 */
	private ArrayList<String> paper;	
	/**
	 * 
	 */
	private FileParser file_Parser;
	
	/**
	 * 
	 */
	public SubProgramChair(){
		
	}
	
	/**
	 * @param file_Parser
	 * @param a_name
	 * @param a_conference
	 */
	public SubProgramChair(FileParser file_Parser, String a_name, Conference a_conference) {
		super(file_Parser, a_name, a_conference);
		conference = a_conference;
		name = a_name;
		this.file_Parser = file_Parser;
	}
	
	/**
	 * @param file_Parser
	 * @param a_name
	 * @param a_conference
	 * @param a_email
	 * @param a_phone
	 */
	public SubProgramChair(FileParser file_Parser, String a_name, Conference a_conference, String a_email, String a_phone) {
		super(a_name, a_conference, a_email, a_phone);
		this.file_Parser = file_Parser;
	}
	
	
	/**
	 * @return
	 */
	public ArrayList<String> getPaper() {
		return paper;
	}
	/**
	 * @param paper
	 */
	public void setPaper(ArrayList<String> paper) {
		this.paper = paper;
	}	
	
	/**
	 * @return
	 */
	public ArrayList<Manuscript> getManuscripts() {
		
		 return file_Parser.getManuscripts(Roles.SUBPC.toString(), name, conference.getName()); 
	}
	
	/**
	 * @param a_name
	 * @return
	 */
	public TreeSet<String> getUserList(final String a_name){
		return file_Parser.getUserList(a_name);
	}
	
	/**
	 * @param table
	 * @return
	 */
	public String viewManuscript(final JTable table) {
		String manuscript = "";
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
							//must cast to correct TableModel, or getManuscriptLink wont work.

				
			//String link = ((TemplateTableModel)table.getModel()).getManuscriptLink(row);//get link	
			String link = ((SPCTemplate)table.getModel()).getManuscriptLink(row);//get link
			InputStream stream = FileParser.class.getResourceAsStream(link);	//convert to stream
			try { 
					XWPFDocument docx = new XWPFDocument(stream);				//parse docx
					XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
					manuscript = we.getText();				
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
					stream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return manuscript;
	}
	
	/**
	 * @param a_reviewer
	 * @param a_manuscript
	 */
	public void assignPaper(String a_reviewer, Manuscript a_manuscript){
		
		file_Parser.assignPaper(a_reviewer, a_manuscript);
		
	}
	/**
	 * @param a_title
	 * @param a_recVal
	 */
	public void submitRecommendation(final String a_title, final int a_recVal) {
		
		file_Parser.submitRecommendation(a_title, a_recVal);
			 
	}
	/**
	 * @param table
	 * @return
	 */
	public ArrayList<String> viewReview(final JTable table) {
		ArrayList<String> review = new ArrayList<String>();
		if(table.getSelectedRow() != -1) {
			//convert to models row index, since the table index changes when it is sorted
			int row = table.convertRowIndexToModel(table.getSelectedRow());
			//String link = ((TemplateTableModel)table.getModel()).getManuscriptLink(row);//get link	
			ArrayList<Review> links = ((SPCTemplate)table.getModel()).getReviews(row);//get link
			if(!links.isEmpty()) {
				for(int i = 0; i < links.size(); i++) {
					if(!links.get(i).getReviewLink().equalsIgnoreCase("")){
						InputStream stream = FileParser.class.getResourceAsStream(links.get(i).getReviewLink());	//convert to stream
						try { // apache poi documentation  http://poi.apache.org/apidocs/index.html
								XWPFDocument docx = new XWPFDocument(stream);				//parse docx
								XWPFWordExtractor we = new XWPFWordExtractor(docx);			//extract parsed docx
								review.add(i, we.getText());						
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						try {
								stream.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		}
		return review;
	}

}
